<template>
	<view >
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar  @input="input"  radius="100" cancelButton="none">
			</uni-search-bar>
		</view>
		
		<!-- 搜索建议列表 -->
		<view class="suggest-list" v-if="searchList.length  !== 0">
			<view class="suggest-list-item" v-for="(item, index) in searchList" :key="index" @click="toDetail(item.goods_id)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16" ></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="history-list-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="clearHistoryList"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item, index) in historyList" :key="index" @click="toGoodsList" >
				</uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				// 搜索关键词
				kw: '',
				// 搜索建议列表
				searchList: [],
				// 搜索历史
				historyList: []
			};
		},
		
		methods: {
			// 防抖处理
			input(e) {
				// console.log(e);
				// 清除定时器
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					// console.log(this.kw);
					// 获取搜索建议列表
					this.getSearchList()
				}, 500)
			},
			
			// 获取搜索建议列表
			async getSearchList() {
				// 如果没有关键词，让searchList为空
				if(this.kw === '') {
					return this.searchList = []
				}
				const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw})
				// console.log(res);
				if(res.meta.status !== 200) return uni.$showMsg()
				this.searchList = res.message
				// 获取关键字
				this.saveHistoryList()
				
			},
			
			// 跳转到详情页
			toDetail(id) {
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + id
				})
			},
			
			// 保存搜索历史
			saveHistoryList() {
				this.historyList.unshift(this.kw)
				// 数组去重
				let set = new Set(this.historyList)
				// console.log(set);
				this.historyList = Array.from(set)
				// 将历史记录存储到浏览器内存里
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			
			// 清除搜索历史
			clearHistoryList() {
				this.historyList = [],
				uni.setStorageSync('kw', '[]')
			},
			
			// 点击搜索历史跳转到商品页面
			toGoodsList() {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + this.kw
				})
			}
		},
		
		onLoad() {
			// 页面加载时，加载本地存储记录
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		}
		
	}
</script>

<style lang="scss" scoped>
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}
	.suggest-list {
		padding: 0 5px;
		.suggest-list-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}
	.history-box {
		padding: 0 5px;
		.history-list-title {
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
		.history-list {
			display: flex;
			flex-wrap: wrap;
			margin-top: 5px;
			.uni-tag {
				margin-right: 5px;
			}
		}
	}
</style>
